package cn.wizzer.app.web.modules.controllers.platform.sys;

import cn.wizzer.app.sys.modules.models.SysMsg;
import cn.wizzer.app.sys.modules.services.SysMsgService;
import cn.wizzer.app.sys.modules.services.SysMsgUserService;
import cn.wizzer.app.sys.modules.services.SysUserService;
import cn.wizzer.app.user.modules.services.UserService;
import cn.wizzer.app.web.commons.ext.websocket.WkNotifyService;
import cn.wizzer.app.web.commons.ulog.annotation.SLog;
import cn.wizzer.app.web.commons.utils.PageUtil;
import cn.wizzer.app.web.commons.utils.StringUtil;
import cn.wizzer.framework.util.Result;
import cn.wizzer.framework.page.Pagination;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.Sqls;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Lang;
import org.nutz.lang.Strings;
import org.nutz.lang.Times;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@IocBean
@At("/platform/sys/msg")
public class SysMsgController {
    private static final Log log = Logs.get();

    @Inject
    @Reference
    private SysMsgService sysMsgService;

    @Inject
    @Reference
    private SysMsgUserService sysMsgUserService;

    @Inject
    @Reference
    private SysUserService sysUserService;

    @Inject
    @Reference
    private UserService userService;

    @Inject
    private WkNotifyService wkNotifyService;

    @At({"/", "/list/?"})
    @Ok("beetl:/platform/sys/msg/index.html")
    @RequiresPermissions("sys.manager.msg")
    public void index(String type, HttpServletRequest req) {
        req.setAttribute("type", Strings.isBlank(type) ? "all" : type);
    }

    @At
    @Ok("json:full")
    @RequiresPermissions("sys.manager.msg")
    public Object data(@Param("searchType") String searchType, @Param("pageNumber") int pageNumber, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(searchType) && !"all".equals(searchType)) {
            cnd.and("type", "=", searchType);
        }
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        }
        List<Map> mapList = new ArrayList<>();
        Pagination pagination = sysMsgService.listPage(pageNumber, pageSize, cnd);
        for (Object msg : pagination.getList()) {
            NutMap map = Lang.obj2nutmap(msg);
            map.put("all_num", sysMsgUserService.count(Cnd.where("msgId", "=", map.get("id", ""))));
            map.put("unread_num", sysMsgUserService.count(Cnd.where("msgId", "=", map.get("id", "")).and("status", "=", 0)));
            mapList.add(map);
        }
        pagination.setList(mapList);
        return Result.success().addData(pagination);
    }

    @At
    @Ok("json:full")
    @RequiresPermissions("sys.manager.msg")
    public Object edit(@Param("id") String id) {
        return Result.success().addData(sysMsgService.fetch(id));
    }

    @At
    @Ok("json:full")
    @RequiresPermissions("sys.manager.msg")
    public Object editDo(@Param("..") SysMsg msg) {
        return Result.success().addData(sysMsgService.updateIgnoreNull(msg));
    }

    @At
    @Ok("json:full")
    @RequiresPermissions("sys.manager.msg")
    public Object user_view_data(@Param("type") String type, @Param("id") String id, @Param("pageNumber") int pageNumber, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) {
        Cnd cnd = Cnd.NEW();
        String sql = "SELECT a.loginname,a.username,a.mobile,a.email,a.disabled,a.unitid,b.name as unitname,c.status,c.readat FROM sys_user a,sys_unit b,sys_msg_user c WHERE a.unitid=b.id \n" +
                "and a.loginname=c.loginname and c.msgId='" + id + "' ";
        if (Strings.isNotBlank(type) && "unread".equals(type)) {
            sql += " and c.status=0 ";
        }
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            sql += " order by a." + pageOrderName + " " + PageUtil.getOrder(pageOrderBy);
        }
        return Result.success().addData(sysMsgService.listPage(pageNumber, pageSize, Sqls.create(sql)));
    }

    @At("/addDo")
    @Ok("json")
    @RequiresPermissions("sys.manager.msg.add")
    @SLog(tag = "站内消息", msg = "${args[0].title}")
    public Object addDo(@Param("..") NutMap nutMap) {
        SysMsg sysMsg = nutMap.getAs("msg", SysMsg.class);
        sysMsg.setNote(nutMap.getString("note", ""));
        sysMsg.setSendAt(Times.getTS());
        sysMsg.setOpBy(StringUtil.getPlatformUid());
        String[] users = StringUtils.split(nutMap.getString("users", ""), ",");
        SysMsg sys_msg = sysMsgService.saveMsg(sysMsg, users);
        //websocket通知管理员
        if (sys_msg != null && (sys_msg.getType().equals("user") || sys_msg.getType().equals("system"))) {
            wkNotifyService.notify(sys_msg, users);
        }
        sysMsgUserService.clearCache();
        return Result.success();
    }


    @At
    @Ok("json:{locked:'password|salt',ignoreNull:false}")
    @RequiresPermissions("sys.manager.user")
    public Object user_data(@Param("searchUnit") String searchUnit, @Param("searchName") String searchName, @Param("searchKeyword") String searchKeyword, @Param("pageNumber") int pageNumber, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(searchName) && Strings.isNotBlank(searchKeyword)) {
            cnd.and(searchName, "like", "%" + searchKeyword + "%");
        }
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        }
        return Result.success().addData(sysUserService.listPageLinks(pageNumber, pageSize, cnd, "unit"));
    }

    /**
     * 查询用户列表
     *
     * @param userType      Integer 用户类型
     * @param searchKeyword String 搜索关键字
     * @param pageNumber    int
     * @param pageSize      int
     * @return Object
     */
    @At
    @Ok("json:{locked:'password|salt',ignoreNull:false}")
    @RequiresPermissions("user.manager.list")
    public Object user_list(@Param("userType") Integer userType, @Param("searchKeyword") String searchKeyword, @Param("pageNumber") int pageNumber, @Param("pageSize") int pageSize) {
        try {
            return Result.success().addData(userService.userListNew("", searchKeyword, "", null, pageNumber, pageSize));
        } catch (Exception e) {
            return Result.error();
        }
    }

    @At({"/delete/?"})
    @Ok("json")
    @RequiresPermissions("sys.manager.msg.delete")
    @SLog(tag = "站内消息", msg = "站内信标题:${req.getAttribute('title')}")
    public Object delete(String id, HttpServletRequest req) {
        SysMsg fetch = sysMsgService.fetch(id);
        req.setAttribute("title", fetch.getTitle());
        if (!fetch.getDelFlag()) {
            sysMsgService.deleteMsg(id);
            sysMsgUserService.clearCache();
        } else {
            sysMsgService.update(Chain.make("delFlag", 0), Cnd.where("id", "=", id));
        }
        return Result.success();
    }


}
